package com.casic.modules.jy.service.impl;

import com.casic.common.exception.RRException;
import com.casic.common.utils.R;
import com.casic.modules.jy.entity.JyXsjbxxEntity;
import com.casic.modules.jy.service.JyXsjbxxService;
import com.casic.modules.sys.entity.SysUserEntity;
import com.casic.modules.sys.service.SysUserService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.casic.common.base.service.impl.BaseServiceImpl;

import com.casic.modules.jy.dao.JyRklsDao;
import com.casic.modules.jy.entity.JyRklsEntity;
import com.casic.modules.jy.service.JyRklsService;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;


@Service("jyRklsService")
public class JyRklsServiceImpl extends BaseServiceImpl<JyRklsDao,JyRklsEntity>  implements JyRklsService {
    @Autowired
    private JyXsjbxxService jyXsjbxxService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    public void setDao(JyRklsDao jyRklsDao) {
        this.dao=jyRklsDao;
    }

    @Override
    public List<JyRklsEntity> selectByGh(String gh) {
        return this.dao.selectByGh(gh);
    }

    public boolean addFromExcel(String fileName, MultipartFile file) throws Exception {

        boolean notNull = false;
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new RRException("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        if(sheet!=null){
            notNull = true;
        }
        JyRklsEntity jyRklsEntity;
        SysUserEntity sysUserEntity;
        StringBuffer warnMsg = new StringBuffer("");
        List<JyRklsEntity> rklsList = new ArrayList<JyRklsEntity>();
        List<SysUserEntity> userList = new ArrayList<SysUserEntity>();
        for (int r = 1; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }

            jyRklsEntity = new JyRklsEntity();
            sysUserEntity = new SysUserEntity();


            String name = checkCell(row, 0);
            if(name == null || name.isEmpty()){
                warnMsg.append("导入失败(第"+(r+1)+"行,姓名未填写)<br>");
                continue;
            }

            String gh = checkCell(row, 1);
            if(gh==null || gh.isEmpty()){
                warnMsg.append("导入失败(第"+(r+1)+"行,工号未填写)<br>");
                continue;
            }
            List<JyRklsEntity> lsList = this.dao.selectByGh(gh);
            if(lsList != null && lsList.size() > 0){
                warnMsg.append("导入失败(第"+(r+1)+"行,工号在教师信息表中已存在！<br>");
                continue;
            }
            List<JyXsjbxxEntity> xhList = jyXsjbxxService.selectByXh(gh);
            if(xhList != null && xhList.size() > 0){
                warnMsg.append("导入失败(第"+(r+1)+"行,工号在学生信息表中已存在！<br>");
                continue;
            }
            SysUserEntity user = sysUserService.queryByUserName(gh);
            if(user != null){
                warnMsg.append("导入失败(第"+(r+1)+"行,工号在登录用户信息表中已存在！<br>");
                continue;
            }
            String xb = checkCell(row, 2);
            String xl = checkCell(row, 3);
            String bz = checkCell(row, 4);
            String bm = checkCell(row, 5);
            String yx = checkCell(row, 6);
            String sjh = checkCell(row, 7);

            jyRklsEntity.setId(createId());
            jyRklsEntity.setXb("男".equals(xb)?"0":"1");
            jyRklsEntity.setBz(bz);
            jyRklsEntity.setGh(gh);
            jyRklsEntity.setXl("研究生".equals(xl)?"0":("本科".equals(xl)?"1":("大专".equals(xl)?"2":("中专".equals(xl)?"4":"其他"))));
            jyRklsEntity.setCreateDate(new Date());
            jyRklsEntity.setCreateUserId("1");
            jyRklsEntity.setDeleteState("0");
            rklsList.add(jyRklsEntity);

            sysUserEntity.setUserId(createId());
            sysUserEntity.setUsername(gh);
            sysUserEntity.setPassword("123456");
            sysUserEntity.setNickName(name);
            sysUserEntity.setEmail(yx);
            sysUserEntity.setMobile(sjh);
            sysUserEntity.setDeptId(bm);
            sysUserEntity.setStatus(1);
            List<String> roleList = new ArrayList<String>();
            roleList.add("9c9f8a5d80f9469ca18483c72d6b9971");
            sysUserEntity.setRoleIdList(roleList);
            if(bm.contains(",")){
                String[] depts=bm.split(",");
                List<String> deptsList =new ArrayList<>(Arrays.asList(depts));
                sysUserEntity.setDeptIdList(deptsList);
            }else{
                List<String> deptsList=new ArrayList<>();
                deptsList.add(bm);
                sysUserEntity.setDeptIdList(deptsList);
            }
            userList.add(sysUserEntity);
        }
        if(warnMsg.toString().length() > 0){
            throw new RRException(warnMsg.toString());
        }
        for (JyRklsEntity rkls : rklsList) {
            this.dao.save(rkls);
        }
        for(SysUserEntity user : userList){
            sysUserService.save(user);
        }
        return notNull;
    }

    private String checkCell(Row row, int cellNumber){
        String returnStr = null;
        Cell cell = row.getCell(cellNumber);
        if(cell != null){
            row.getCell(cellNumber).setCellType(Cell.CELL_TYPE_STRING);
            returnStr = row.getCell(cellNumber).getStringCellValue();
        }

        return returnStr;
    }
}
